<!DOCTYPE html>
<html>
	{include file="public1/_header" /}
	<!-- 配置文件 -->
	<script type="text/javascript" src="__STATIC__/static/ueditor/ueditor.config.js"></script>
	<!-- 编辑器源码文件 -->
	<script type="text/javascript" src="__STATIC__/static/ueditor/ueditor.all.js"></script>
	<body style="background: #fff;">
		<div>
			<div class="layui-row">
				<div class="layui-col-md12">
					<form class="layui-form" action="">
						<div class="layui-card">
							<div class="layui-card-body">


								<div class="layui-col-md12" style="padding-bottom: 65px;">
									<table class="layui-table table-form">
										<colgroup>
											<col width="100">
											<col>
										</colgroup>
										<tbody>
											<tr>
												<th style="text-align:right">标题 <span style='color:red'>*</span></th>
												<td>
													<input lay-reqText="标题必须填写" type="text" name="title" required lay-verify="required" autocomplete="off"
													 class="layui-input title">
												</td>
											</tr>
											<tr>
												<th style="text-align:right">内 容 <span style='color:red'>*</span></th>
												<td>
													<textarea name="content" class="layui-textarea" lay-verify="required"></textarea>
												</td>
											</tr>
											<tr>
												<th style="text-align:right">启动屏保时间</th>
												<td><select name="off_time" lay-verify="required">
														<option value="1">1分钟无操作时</option>
														<option value="2">2分钟无操作时</option>
														<option value="3" selected>3分钟无操作时</option>
														<option value="5">5分钟无操作时</option>
														<option value="30">30分钟无操作时</option>
														<option value="60">1小时无操作时</option>
														<option value="120">2小时无操作时</option>
														<option value="180">3小时无操作时</option>
													</select></td>
											</tr>

											<tr>
												<th style="text-align:right">时间段</th>
												<td>
													<input type="text" readonly="" id="end_time" name="start_time" placeholder="" autocomplete="off" class="layui-input">
												</td>
											</tr>
											<tr>
												<th style="text-align:right">日期</th>
												<td><input type="text" readonly="" id="start_time" name="start_date" required placeholder="" autocomplete="off"
													 class="layui-input"></td>
											</tr>
											<tr>
												<th style="text-align:right">生效日期</th>
												<td>
													<input type="checkbox" lay-skin="primary" id="c_all" lay-filter="c_all" title="全  部">
													<input type="checkbox" name="week[]" class="weekId" lay-filter="c_one" lay-skin="primary" value="1" title="星期一">
													<input type="checkbox" name="week[]" class="weekId" lay-filter="c_one" lay-skin="primary" value="2" title="星期二">
													<input type="checkbox" name="week[]" class="weekId" lay-filter="c_one" lay-skin="primary" value="3" title="星期三">
													<input type="checkbox" name="week[]" class="weekId" lay-filter="c_one" lay-skin="primary" value="4" title="星期四">
													<input type="checkbox" name="week[]" class="weekId" lay-filter="c_one" lay-skin="primary" value="5" title="星期五">
													<input type="checkbox" name="week[]" class="weekId" lay-filter="c_one" lay-filter="c_one" value="6"
													 lay-skin="primary" title="星期六">
													<input type="checkbox" name="week[]" class="weekId" lay-filter="c_one" lay-skin="primary" value="0" title="星期日">
												</td>
											</tr>

											<tr>
												<th style="text-align:right">排序</th>
												<td>
													<input type="number" value="100" name="sort" required lay-verify="required" autocomplete="off" class="layui-input">
												</td>
											</tr>
										</tbody>
									</table>
								</div>

								<div style="text-align: center;" class="layui-layout-admin">
									<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
									<button type="button" id="close" class="layui-btn layui-btn-primary">放弃</button>
								</div>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		$(function() {
			layui.extend({
				dtree: 'dtree/dtree'
			}).use(['form', 'upload', 'dtree', 'laydate', 'tree', 'util'], function() {
				var form = layui.form;
				var $ = layui.jquery;
				var laydate = layui.laydate;
				var tree = layui.tree;
				var util = layui.util;
				var dtree = layui.dtree;
				form.verify({
					varVerify: function(value, item) { //value：表单的值、item：表单的DOM对象
						if (!new RegExp("^[a-zA-Z]([-_a-zA-Z0-9]{3,20})$").test(value)) {
							return '必须以字母开头的3-20位英文字符串';
						}
					}
				});

				laydate.render({
					elem: '#start_time',
					type: 'date',
					range: '~',
					format: 'yyyy-MM-dd'
				});
				laydate.render({
					elem: '#end_time',
					type: 'time',
					range: '~',
					format: 'HH:mm'
				});

				//全选
				form.on('checkbox(c_all)', function(data) {
					var a = data.elem.checked;
					if (a == true) {
						$(".weekId").prop("checked", true);
						form.render('checkbox');
					} else {
						$(".weekId").prop("checked", false);
						form.render('checkbox');
					}

				});
				//有一个未选中全选取消选中
				form.on('checkbox(c_one)', function(data) {
					var item = $(".weekId");
					for (var i = 0; i < item.length; i++) {
						if (item[i].checked == false) {
							$("#c_all").prop("checked", false);
							form.render('checkbox');
							break;
						}
					}
					//如果都勾选了  勾上全选
					var all = item.length;
					for (var i = 0; i < item.length; i++) {
						if (item[i].checked == true) {
							all--;
						}
					}
					if (all == 0) {
						$("#c_all").prop("checked", true);
						form.render('checkbox');
					}
				});


				//监听提交
				form.on('submit(formDemo)', function(obj) {
					var index = parent.layer.getFrameIndex(window.name);
					var lfc = top.layerFrameConfig;
					var info = lfc.iframeB.info;
					obj.field.type = 1;
					console.log(obj.field);
					// 获取选中的分类值
					var check_arr = "";
					$('.weekId:checked').each(function() {
						check_arr += $(this).val() + ","
					});
					if (check_arr.length == 0) {
						layer.msg("还未选择生效日期", {
							icon: 7,
							time: 1500
						});
						return false;
					} else {
						check_arr = check_arr.substr(0, check_arr.length - 1)
					}
					obj.field.switching_data = check_arr;
					$.post("{:url('add')}", obj.field, function(res) {
						if (res.status == 1) {
							layer.msg('添加成功', {
								icon: 1,
								time: 300
							}, function() {
								parent.layer.close(index); //再执行关闭
								info.reload()
							});

						} else {
							layer.msg(res.message)
						}
					})
					return false;
				});


				$("#close").click(function() {
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);
				})

			});


		})
	</script>
</html>
